﻿using FPSClient.bll;
using FPSClient.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FPSClient.ui
{
    /// <summary>
    /// CappTreePanel.xaml 的交互逻辑
    /// </summary>
    public partial class CappTreePanel : UserControl
    {
        public delegate void OnSelectedChangedHandler(object sender, TreeViewItem e);

        public event OnSelectedChangedHandler OnSelectedChanged;
        public CappTreePanel()
        {
            InitializeComponent();
            init();
        }

        private void init()
        {
            CappInfoManager.Instance.OnCappInfoChanged += Instance_OnCappInfoChanged;
        }

        private void Instance_OnCappInfoChanged(QMFawTechnicsInfo oldCappInfo, QMFawTechnicsInfo newCappInfo)
        {
            BuildTree();
        }

        private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView tv = sender as TreeView;
            if (tv != null && tv.Name.CompareTo("treeView") == 0)
            {
                TreeViewItem selectedItem = e.NewValue as TreeViewItem;
                if (selectedItem != null)
                {
                    if (OnSelectedChanged != null)
                    {
                        OnSelectedChanged(this, selectedItem);
                    }
                }
            }
        }

        private void BuildTree()
        {
            treeView.Items.Clear();
            foreach (ProcedureInfo step in CappInfoManager.Instance.GetCurrentProcedureInfos())
            {
                TreeViewItem stepItem = new TreeViewItem();
                stepItem.Header = string.Format("{0}. {1}", step.StepNumber, step.StepName);
                stepItem.Tag = step;
                stepItem.IsExpanded = true;
                foreach (ProcedureInfo pace in step.paces)
                {
                    TreeViewItem paceItem = new TreeViewItem();
                    paceItem.Header = string.Format("{0}. {1}", pace.StepNumber, pace.StepName);
                    paceItem.Tag = pace;
                    paceItem.IsExpanded = true;
                    foreach (zuoyemiaoshuInfo zuoyemiaoshu in pace.zuoyemiaoshus)
                    {
                        TreeViewItem zuoyemiaoshuItem = new TreeViewItem();
                        zuoyemiaoshuItem.Header = string.Format("{0}. {1}", zuoyemiaoshu.XUHAO, zuoyemiaoshu.zuoyemiaoshuName);
                        zuoyemiaoshuItem.Tag = zuoyemiaoshu;
                        zuoyemiaoshuItem.IsExpanded = true;
                        paceItem.Items.Add(zuoyemiaoshuItem);
                    }
                    stepItem.Items.Add(paceItem);
                }
                treeView.Items.Add(stepItem);
            }
        }
    }
}
